<?php
namespace Admin\Controller;
use Think\Controller;
class UserController extends CommonController {
	//用户列表
    public function userList(){
        // 统计总数
        $count = M()->query("select count(id) count from user ")[0]['count'];
        $Page       = new \Think\Page($count,3);// 实例化分页类 传入总记录数和每页显示的记录数
        $show       = $Page->show();// 分页显示输出
        $sql = "select * from user limit {$Page->firstRow},{$Page->listRows}";
        $result = M()->query($sql);
        foreach ($result as $key => $value) {
           
            switch ($value['sex']) {
                case 0:
                    $result[$key]['sex'] = '保密';
                    break;
                case 1:
                    $result[$key]['sex'] = '男';
                    break;
                case 2:
                    $result[$key]['sex'] = '女';
                    break;
            }
            $result[$key]['now_address'] = $result[$key]['city'].$result[$key]['addr'];
            $result[$key]['now_address'] = ($result[$key]['now_address'] != '') ? $result[$key]['now_address'] : '<span style="color:red">此人未填写地址</span>';
            $result[$key]['reg_time'] = date("Y-m-d h:i:s",$result[$key]['reg_time']);
        }
        $this->assign('Page',$show); //渲染分页
        $this->assign('count',$count);
        $this->assign('userdata',$result);
        $this->display();
    }

    // 被删除的用户
    public function userDeled(){
    	$this->display();
    }

    

    //停用用户的异步方法
    public function userstop(){
        if(IS_AJAX){
           $sql = "update user set status = '-1' where id = {$_POST['userid']}";
           M()->execute($sql);
           echo json_encode(1);
        }
    }
    //启用用户的异步方法
    public function userstart(){
        if(IS_AJAX){
           $sql = "update user set status = '1' where id = {$_POST['userid']}";
           M()->execute($sql);
           echo json_encode(1);
        }
    }

    //浏览记录
    public function recordBrowse(){
    	$this->display();
    }

    //下载记录
    public function recordDownload(){
    	$this->display();
    }

    //分享记录
    public function recordShare(){
    	$this->display();
    }

    //用户添加
    public function userAdd(){
    	$this->display();
    }

    //用户编辑
    public function userUpdate(){
        if($_GET){
            $id = $_GET['uid'];
            $userSql = "select * from user where id =  {$id}";
            $res = M()->query($userSql);
            $this->assign('userData',$res);
            $this->display();
        }
    	
    }
    public function ajaxuserface(){
        if(IS_AJAX){
            $userid = $_POST['theid'];
            $userface = M('user')->where("id = {$userid}")->getField('face');
            $returnMsg = array(
                'error' => 0,
                'img' => $userface,
            );
            $this->ajaxReturn($returnMsg);
        }
    }

    //用户密码修改
    public function userPwdUpdate(){
    	$this->display();
    }

    //用户删除
    public function userDelete(){
    	$this->display();
    }

    //资料修改保存
    public function editsave(){
        if(IS_POST){ 
            var_dump($_FILES);
            exit;
            $data['id'] = $_POST['userid'];
            $data['username'] = $_POST['username'];
            $data['sex'] = $_POST['sex'];
            $data['password'] = $_POST['password'];
            $data['status'] = $_POST['status'];
            $data['phone'] = $_POST['phone'];
            $data['email'] = $_POST['email'];
            $data['face'] = $_POST['oldface'];
            $data['signnature'] = $_POST['signnature'];
            //如果有选择头像
            if($_FILES['facefile']['name'] != ''){
                $upload = new \Think\Upload();// 实例化上传类
                $upload->maxSize   =     3145728 ;// 设置附件上传大小
                $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
                $upload->rootPath  =     './Uploads/'; // 设置附件上传根目录
                $upload->savePath  =     'images/'; // 设置附件上传（子）目录
                // 上传文件 
                $info   =   $upload->upload();

                if(!$info) {// 上传错误提示错误信息
                    $this->error($upload->getError());
                }else{// 上传成功
                    // 'Uploads/'.$value['savepath'].'/'.$value['savename'];
                    foreach ($info as $key => $value) {
                        $face = 'Uploads/'.$value['savepath'].$value['savename'];
                    }
                    $data['face'] = $face;
                }
            }
            if(M('user')->save($data)!==false){
                $this->success('修改成功',U('admin/user/userlist'));
            }else{
                $this->error('修改失败');
            }

        }
    }

    //添加用户
    public function useraddsave(){
        if(IS_POST){ 
            $data['id'] = $_POST['userid'];
            $data['username'] = $_POST['username'];
            $data['sex'] = $_POST['sex'];
            $data['password'] = $_POST['password'];
            $data['status'] = $_POST['status'];
            $data['phone'] = $_POST['phone'];
            $data['email'] = $_POST['email'];
            $data['signnature'] = $_POST['signnature'];
            //如果有选择头像
            if($_FILES['facefile']['name'] != ''){
                $upload = new \Think\Upload();// 实例化上传类
                $upload->maxSize   =     3145728 ;// 设置附件上传大小
                $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
                $upload->rootPath  =     './Uploads/'; // 设置附件上传根目录
                $upload->savePath  =     'images/'; // 设置附件上传（子）目录
                // 上传文件 
                $info   =   $upload->upload();

                if(!$info) {// 上传错误提示错误信息
                    $this->error($upload->getError());
                }else{// 上传成功
                    foreach ($info as $key => $value) {
                        $face = 'Uploads/'.$value['savepath'].$value['savename'];
                    }
                    $data['face'] = $face;
                }
            }
            if(M('user')->add($data)){
                $this->success('增加成功',U('admin/user/userlist'));
            }else{
                $this->error('增加失败');
            }

        }
    }

    //删除用户的方法
    public function thisuserdelete(){
        $sql = "delete from user where id = {$_GET['id']}";
        if(M()->execute($sql)){
            $this->success('删除成功',U('admin/user/userlist'));
        }else{
            $this->error('增加失败');
        }
    }

    //展示用户详情的方法
    public function userdetail(){
        if($_GET){
            $id = $_GET['uid'];
            $userSql = "select * from user where id =  {$id}";
            $res = M()->query($userSql);
            $this->assign('userData',$res);
            $this->display();
        }
        
    }

}
?>